﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.Commands.SheepFarmCommands
{
    public class SheepFarmCreatHandler : IRequestHandler<SheepFarmCreateCommand, Result>
    {
        private readonly IBaseRepository<SheepFarm> sheepfarmRep;


        public SheepFarmCreatHandler(IBaseRepository<SheepFarm> sheepfarmRep)
        {
            this.sheepfarmRep = sheepfarmRep;
        }

        public async Task<Result> Handle(SheepFarmCreateCommand request, CancellationToken cancellationToken)
        {
            var query = new Result() { Code = (int)CodeEnum.success };

            #region 验证
            var w1 = sheepfarmRep.GetAll().Where(x => x.FarmName == request.AddSheepFarm.FarmName).Count();
            if (w1 > 0)
            {
                query.Code = -1;
                query.Messege = "该羊场名称已存在";
                return query;
            }

            var w3 = sheepfarmRep.GetAll().Where(x => x.FarmAddress == request.AddSheepFarm.FarmAddress).Count();
            if (w3 > 0)
            {
                query.Code = -1;
                query.Messege = "该详细地址已存在";
                return query;
            }


            var w10 = sheepfarmRep.GetAll().Where(x => x.SheepFarmNick == request.AddSheepFarm.SheepFarmNick).Count();
            if (w10 > 0)
            {
                query.Code = -1;
                query.Messege = "该羊场简称已存在";
                return query;
            }
            #endregion

            var list = new SheepFarm()
            {
                FarmId = request.AddSheepFarm.FarmId,
                SerialId = request.AddSheepFarm.SerialId,
                SheepFarmNo = request.AddSheepFarm.SheepFarmNo,
                FarmName = request.AddSheepFarm.FarmName,
                BuildingDate = request.AddSheepFarm.BuildingDate = DateTime.Now,
                FarmAddress = request.AddSheepFarm.FarmAddress,
                FarmState = request.AddSheepFarm.FarmState,
                CreateName = request.AddSheepFarm.CreateName = "zhangsan",
                CreateDate = request.AddSheepFarm.CreateDate = DateTime.Now,
                UpdateName = request.AddSheepFarm.UpdateName = "zhangsan",
                UpdateDate = request.AddSheepFarm.UpdateDate = DateTime.Now,
                IsDelete = request.AddSheepFarm.IsDelete = false,
                HeadID = request.AddSheepFarm.HeadID,
                DepartmentId = request.AddSheepFarm.DepartmentId,
                ProvinceId = request.AddSheepFarm.ProvinceId,
                CityId = request.AddSheepFarm.CityId,
                CountyId = request.AddSheepFarm.CountyId,
                SheepFarmNick = request.AddSheepFarm.SheepFarmNick,
                Scale = request.AddSheepFarm.Scale
            };

            if (list != null)
            {
                var result = sheepfarmRep.Add(list);
                query.Code = 1;
                query.Data = result;
            }

            return query;
        }
    }
}
